package facelets.convert;

import javax.el.ELException;
import javax.faces.FacesException;
import javax.faces.convert.Converter;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.FaceletException;
import com.sun.facelets.tag.TagAttribute;
import com.sun.facelets.tag.TagConfig;
import com.sun.facelets.tag.jsf.ConvertHandler;
import com.sun.facelets.tag.jsf.ConverterConfig;

import es.xpt.ysf.convert.EnumConverter;

public class EnumConverterHandler extends ConvertHandler {
	
    private final TagAttribute type;
    
    /**
     * @param config
     */
    public EnumConverterHandler(ConverterConfig config) {
        super(config);
        this.type = this.getRequiredAttribute("type");
    }
    
    @SuppressWarnings("deprecation")
	public EnumConverterHandler(TagConfig config) {
        super(config);
        this.type = this.getRequiredAttribute("type");
    }
    
    /**
     * Returns a new EnumConverter
     * 
     * @see EnumConverter
     * @see com.sun.facelets.tag.jsf.ConvertHandler#createConverter(com.sun.facelets.FaceletContext)
     */
    protected Converter createConverter(FaceletContext ctx)
            throws FacesException, ELException, FaceletException {
        return ctx.getFacesContext().getApplication().createConverter(EnumConverter.CONVERTER_ID);
    }


    /**
     * Implements tag spec, see taglib documentation.
     * 
     * @see com.sun.facelets.tag.ObjectHandler#setAttributes(com.sun.facelets.FaceletContext,
     *      java.lang.Object)
     */
    protected void setAttributes(FaceletContext ctx, Object obj) {
        EnumConverter c = (EnumConverter) obj;  
        c.setType(this.type.getValue(ctx));  
    }
}
